 
//  Created by Open on 3/19/15.
//  Copyright (c) 2015 open-groupe. All rights reserved.
//http://swifter.tips/regex/#operator
//正则表达式 30 分钟入门教程 http://deerchao.net/tutorials/regex/regex.htm
// 8 个常用正则表达 http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149

import Foundation



struct RegexHelper {
    let regex: NSRegularExpression?
    
    init(_ pattern: String) {
        var error: NSError?
        regex = NSRegularExpression(pattern: pattern,
            options: .CaseInsensitive,
            error: &error)
    }
    
    func match(input: String) -> Bool {
        if let matches = regex?.matchesInString(input,
            options: nil,
            range: NSMakeRange(0, strLength(input))) {
                return matches.count > 0
        } else {
            return false
        }
    }
    
    func strLength(string: String) -> Int {
        return string.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
    }
    
    
}

infix operator =~ {
associativity none
precedence 130
}

func =~ (lhs: String, rhs: String) -> Bool {
    return RegexHelper(rhs).match(lhs)
}



